package net.oschina.app.ui;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.FragmentTabHost;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
import android.widget.Toast;

import net.oschina.app.R;
import net.oschina.app.base.BaseActivity;
import net.oschina.app.bean.NewsList;
import net.oschina.app.util.XmlUtils;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {
    //3.1 注解式声明要绑定的控件
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tabhost)
    FragmentTabHost mTabhost;
    //3.2 注解式声明点击事件
    @OnClick(R.id.toolbar)
    public  void testClick(View v){
        Toast.makeText(this, "Toobar点击", Toast.LENGTH_SHORT).show();
    }
    //中间按钮点击事件
    @OnClick(R.id.iv_quick_option)
    public  void quickOption(View v){
        Toast.makeText(this, "点击中间按钮，测试xml解析", Toast.LENGTH_SHORT).show();
        //解析xml数据
        parseXml();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //3.3 通过ButterKnife绑定控件与事件
        ButterKnife.bind(this);
        //1. 初始化ToolBar
        initToolBar();
        //4. 初始化FragmentTabHost
        initTabHost();
    }

    //4 . 初始化TabHost
    private void initTabHost() {
        //4.1 初始化底部标签
        /**
         * 设置TabHost
         * context :上下文
         * manager ：片段管理器
         * containerId:装载片段的容器
         */
        mTabhost.setup(this, getSupportFragmentManager(), R.id.fl_realcontent);
        if(android.os.Build.VERSION.SDK_INT>10){ //大于10版本，不显示分割线
            //标签小部件，是一个View，
            TabWidget tabWidget = mTabhost.getTabWidget();
            //不显示分割线
            tabWidget.setShowDividers(0);
        }

        // 4.2 往FragmentTabHost ：添加tab标签
        addTabs();

    }

    //添加底部导航标签
    private void addTabs() {
        MainTab[] tabs = MainTab.values();
        final int size = tabs.length;
        for (int i = 0; i < size; i++) {
            // 找到每一个枚举的Fragment对象
            MainTab mainTab = tabs[i];
            // 1. 创建标签
            TabHost.TabSpec tab = mTabhost.newTabSpec(this.getResources().getString(mainTab.getResName()));
            // 初始化标签 ↓
            View indicator = LayoutInflater.from(getApplicationContext())
                    .inflate(R.layout.tab_indicator, null);
            TextView title = (TextView) indicator.findViewById(R.id.tab_title);
            Drawable drawable = this.getResources().getDrawable(
                    mainTab.getResIcon());
            //设置TextView上下左右的绘图
            title.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null,
                    null);
            if (i == 2) {
                //当是正中间的标签，则隐藏 ，用于显示ImageView 弹出对话框的控件
                indicator.setVisibility(View.INVISIBLE);
            }
            title.setText(getString(mainTab.getResName()));
            //设置标签的指示器，是一个View
            tab.setIndicator(indicator);

            //2. 设置要传递给Fragment的参数
            Bundle bundle = new Bundle();
            bundle.putString("key",
                    "content: " + getString(mainTab.getResName()));
            // 3. 把新的标签添加到TabHost中
            mTabhost.addTab(tab, mainTab.getClz(), bundle);
        }
    }
    //5. 使用xStream框架实现xml解析
    private void parseXml() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    URL url = new URL("http://10.0.2.2:8080/oschina/list/news/page0.xml");
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.setRequestMethod("GET");
                    conn.setConnectTimeout(5000);
                    conn.connect();
                    int code = conn.getResponseCode();
                    if (code == 200) {
                        //从服务端获取xml数据流
                        InputStream is = conn.getInputStream();
                        NewsList newsList = XmlUtils.toBean(NewsList.class, is);
                        Log.e("xstream",newsList.getList().get(0).getBody());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }


    //1. 初始化ToolBar
    private void initToolBar() {
        //1.1引用toolbar
//        Toolbar toolbar= (Toolbar) findViewById(R.id.toolbar);
        //1.2 用Toolbar取得ActionBar
        setSupportActionBar(toolbar);
        //1.3 设置导航图标及监听
        toolbar.setNavigationIcon(R.drawable.ic_drawer);
/*        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "回退按钮", Toast.LENGTH_SHORT).show();
                finish();
            }
        });*/
    }

    //2.1 创建toolbar的菜单项
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
         getMenuInflater().inflate(R.menu.search_menu,menu);
        return super.onCreateOptionsMenu(menu);
    }
  //2.2 监听ActionBar的菜单项
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home: //actionbar/toobar的导航按钮监听
                Toast.makeText(this, "返回", Toast.LENGTH_SHORT).show();
                break;
            case R.id.search_content: //搜索按钮监听
                Toast.makeText(this, "搜索", Toast.LENGTH_SHORT).show();
                break;
        }
        return super.onContextItemSelected(item);
    }

}
